<template>
  <div class="login-container">
    <div class="logo" />
    <!--登录表单 Stare-->
    <el-form
      ref="loginForm"
      :model="loginForm"
      status-icon
      :rules="rules"
      class="demo-ruleForm"
    >
      <img src="@/assets/common/logo.595745bd.png" alt="" class="beijingtu">
      <el-form-item prop="phone">
        <el-input
          v-model="loginForm.phone"
          type="phone"
          autocomplete="off"
          prefix-icon="el-icon-mobile-phone"
          placeholder="请输入账号"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          class="elinout"
          show-password
          type="password"
          autocomplete="off"
          prefix-icon="el-icon-lock"
          placeholder="请输入密码"
        />
      </el-form-item>
      <el-form-item prop="code">
        <el-input
          v-model="loginForm.code"
          class="elinout yanzma"
          prefix-icon="el-icon-chat-dot-round"
          placeholder="请输入验证码"
        >
          <template slot="append">
            <img :src="picture" alt="" @click="changePic">
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          class="elinout"
          type="primary"
          @click="submitForm"
        >登录</el-button>
      </el-form-item>
    </el-form></div>
  <!-- 登陆表单End -->
</template>
<script>
import nanoid from '@/utils/getRandom'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        phone: process.env.NODE_ENV === 'development' ? 'admin' : '',
        password: process.env.NODE_ENV === 'development' ? 'admin' : '',
        code: ''
      },
      picture: '',
      clientToken: '',
      rules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 3,
            max: 15,
            message: '请输入3到15之间正确的密码',
            trigger: 'blur'
          }
        ],
        code: [
          // 1.效验规则，2.效验的值，3.回调函数(直接调用，表示通过效验)
          // callback(new Error('请勾选协议')) 没有通过效验
          {
            validator: (rule, value, callback) => {
              value ? callback() : callback(new Error('请输入验证码'))
            }
          }
        ]
      }
    }
  },
  async created() {
    console.log(process.env.NODE_ENV)
    this.clientToken = nanoid()
    this.getitem()
  },
  methods: {
    async submitForm() {
      await this.$refs.loginForm.validate()
      await this.$store.dispatch('user/login', {
        loginName: this.loginForm.phone,
        password: this.loginForm.password,
        code: this.loginForm.code,
        clientToken: this.clientToken,
        loginType: 0
      })
      this.$router.push('/')
    },
    changePic() {
      this.clientToken = nanoid()
      this.getitem()
    },
    async getitem() {
      this.picture = `http://likede2-admin.itheima.net/likede/api/user-service/user/imageCode/${this.clientToken}`
    }
  }
}
</script>
<style lang="scss">
.login-container {
  position: relative;
  align-items: stretch;
  height: 100vh;
  .logo {
    background: rgba(38, 72, 176) url(../../assets/common/kelide.png) no-repeat
      center / cover;
    width: 100%;
    height: 100%;
    flex-direction: column;
    align-items: flex-end;
    justify-content: center;
    .icon {
      background: url(../../assets/common/logo.png) no-repeat 70px center /
        contain;
      width: 300px;
      height: 50px;
      margin-bottom: 50px;
    }
  }
  .demo-ruleForm {
    border-radius: 10px;
    position: absolute;
    width: 518px;
    height: 388px;
    background-color: aliceblue;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
    padding: 76px 35px 0;
    .el-input__inner {
      display: flex;
      height: 52px;
      border-radius: 5px;
      // overflow: hidden;
    }
    .elinout {
      width: 100%;
      height: 52px;
      font-size: 16px;
      border-radius: 5px;
      // overflow: hidden;
    }
    .beijingtu {
      position: absolute;
      width: 96px;
      height: 96px;
      top: -46px;
      left: 50%;
      margin-left: -48px;
    }
    .el-input-group__append {
      padding: 0;
      border: none;
      img {
        width: 130px;
        height: 50px;
      }
    }
  }
}
</style>
